﻿
using Company.Database.Entities;

namespace Company.Database.Sqlite
{
    public class UserRepository : RepositoryBase, IUserRepository
    {
        public bool Delete(UserEntity entity)
        {
           db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
            return db.SaveChanges() > 0;
        }

        public List<UserEntity> GetAll()
        {
            return db.Users.ToList();
        }

        public bool Insert(UserEntity entity)
        {
            db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Added;
            return db.SaveChanges() > 0;
        }

        public bool Login(string username, string password, out UserEntity? user)
        {
            user = db.Users.SingleOrDefault(u => u.UserName == username && u.Password == password);
            return user != null;
        }

        public UserEntity? search(string username)
        {
            return db.Users.Where(t => t.UserName == username).FirstOrDefault();
        }

        public bool Update(UserEntity entity)
        {
            db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return db.SaveChanges() > 0;
        }
    }
}
